package com.example.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.model.entity.Grade;
import com.example.model.entity.Course;
import com.example.model.dto.GradeRequest;
import com.example.mapper.GradeMapper;
import com.example.mapper.CourseMapper;
import com.example.model.entity.UserInfo;
import com.example.model.vo.GradeVo;
import com.example.service.GradeService;
import com.example.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;

@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements GradeService {
    
    @Autowired
    private GradeMapper gradeMapper;
    
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private UserService userService;
    
    @Override
    public List<GradeVo> listAllGrades() {
        List<Grade> list = list();
        List<UserInfo> userInfos = userService.studentList();
        Map<Integer, UserInfo> userMap = userInfos.stream().collect(toMap(UserInfo::getId, Function.identity(), (k1, k2) -> k1));
        if(CollUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        return list.stream().map(item -> {
            GradeVo gradeVo = new GradeVo();
            BeanUtils.copyProperties(item,gradeVo);
            UserInfo userInfo = userMap.get(item.getUserId());
            if(ObjectUtil.isNotNull(userInfo)){
                gradeVo.setUserName(userInfo.getUserName());
                gradeVo.setCode(userInfo.getCode());
            }
            return gradeVo;
        }).collect(Collectors.toList());
    }
    
    @Override
    public List<Grade> listStudentGrades(Integer userId) {
        return gradeMapper.findByUserId(userId);
    }
    
    @Override
    public Grade addGrade(GradeRequest gradeRequest) {
        // 检查课程是否存在
        Course course = courseMapper.findById(gradeRequest.getCourseId());
        if (course == null) {
            throw new RuntimeException("课程不存在");
        }
        
        // 检查是否已有成绩
        Grade existGrade = gradeMapper.findByUserIdAndCourseId(
            gradeRequest.getUserId(), 
            gradeRequest.getCourseId()
        );
        if (existGrade != null) {
            throw new RuntimeException("该学生的这门课程已有成绩");
        }
        
        // 添加成绩
        Grade grade = new Grade();
        grade.setUserId(gradeRequest.getUserId());
        grade.setCourseId(gradeRequest.getCourseId());
        grade.setCourseName(course.getCourseName());
        grade.setScore(gradeRequest.getScore());
        
        save(grade);
        return grade;
    }
    
    @Override
    public Grade updateGrade(GradeRequest gradeRequest) {
        // 检查成绩是否存在
        Grade grade = gradeMapper.findByUserIdAndCourseId(
            gradeRequest.getUserId(), 
            gradeRequest.getCourseId()
        );
        if (grade == null) {
            throw new RuntimeException("成绩记录不存在");
        }
        
        // 更新成绩
        grade.setScore(gradeRequest.getScore());
        gradeMapper.update(grade);
        
        return grade;
    }

    @Override
    public Boolean delete(Grade grade) {
        Integer id = grade.getId();
        return removeById(id);
    }
} 